<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.library.entity.po.BookPO" %>
<%@ page import="java.util.List" %>
<%@ page import="com.library.entity.Book" %>
<%@ page import="com.library.entity.po.PagePO" %>
<%@ page import="com.library.entity.Admin" %>
<!DOCTYPE html>
<html>
<head>
    <title>图书管理系统 - 书籍管理</title>
    <style>
        /* 页面整体样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        /* 顶部导航栏样式 */
        .header {
            background-color: #f8f9fa;
            padding: 10px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            margin: 0;
            font-size: 20px;
            color: #333;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .logout-btn {
            margin-left: 15px;
            padding: 5px 10px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            color: #333;
        }

        /* 左侧导航栏样式 */
        .nav-container {
            width: 200px;
            background-color: #f8f9fa;
            float: left;
            min-height: calc(100vh - 50px);
            border-right: 1px solid #e0e0e0;
        }

        .admin-label {
            padding: 15px;
            background-color: #e9ecef;
            font-weight: bold;
            border-bottom: 1px solid #ddd;
        }

        .nav-container ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .nav-container li {
            padding: 15px;
            cursor: pointer;
            border-bottom: 1px solid #e0e0e0;
        }

        .nav-container li:hover {
            background-color: #e9ecef;
        }

        .nav-container li a {
            text-decoration: none;
            color: #333;
            display: block;
        }

        .nav-container li.active {
            background-color: #007bff;
        }

        .nav-container li.active a {
            color: white;
        }

        /* 中间内容区域样式 */
        .content-container {
            margin-left: 200px;
            padding: 20px;
        }

        /* 操作按钮样式 */
        .action-buttons {
            margin-bottom: 20px;
        }

        .btn {
            padding: 8px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            text-decoration: none;
            display: inline-block;
        }

        .btn:hover {
            background-color: #0069d9;
        }

        /* 搜索框样式 */
        .search-box {
            margin-bottom: 20px;
            display: flex;
        }

        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px 0 0 4px;
            width: 300px;
        }

        .search-box button {
            padding: 8px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 0 4px 4px 0;
            cursor: pointer;
        }

        /* 表格样式 */
        .book-table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .book-table th, .book-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .book-table th {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        .book-table tr:hover {
            background-color: #f5f5f5;
        }

        .action-link {
            color: #007bff;
            text-decoration: none;
            margin-right: 10px;
        }

        .action-link:hover {
            text-decoration: underline;
        }

        /* 分页样式 */
        .pagination {
            margin-top: 20px;
            display: flex;
            justify-content: center;
        }

        .pagination a {
            padding: 8px 12px;
            margin: 0 5px;
            border: 1px solid #ddd;
            text-decoration: none;
            color: #007bff;
            border-radius: 4px;
        }

        .pagination a:hover {
            background-color: #f5f5f5;
        }

        .pagination a.active {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }

        .pagination a.disabled {
            color: #aaa;
            cursor: not-allowed;
            background-color: #f5f5f5;
            border-color: #ddd;
        }

        .btn-success {
            background-color: #28a745;
        }

        .btn-success:hover {
            background-color: #218838;
        }

        .btn-danger {
            background-color: #dc3545;
        }

        .btn-danger:hover {
            background-color: #c82333;
        }

        /* 消息提示样式 */
        .alert {
            padding: 10px 15px;
            margin-bottom: 15px;
            border: 1px solid transparent;
            border-radius: 4px;
        }

        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        /* 批量操作表单样式 */
        .batch-form {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<div class="header">
    <h1>图书管理系统</h1>
    <div class="user-info">
        欢迎您，<%=((Admin) request.getSession().getAttribute("admin")).getUsername()%>
        <a href="../common?method=logout" class="logout-btn">退出登录</a>
    </div>
</div>

<!-- 左侧导航栏 -->
<div class="nav-container">
    <div class="admin-label">管理端</div>
    <ul>
        <li><a href="../admin?method=adminIndex">首页</a></li>
        <li class="active"><a href="../admin/book?method=bookManage">书籍</a></li>
        <li><a href="../admin/bookcopy?method=bookCopyManage">书籍副本</a></li>
        <li><a href="../admin/payrec?method=fineRecord">学生罚款记录</a></li>
    </ul>
</div>

<!-- 中间内容区域 -->
<div class="content-container">
    <h2>书籍管理</h2>

    <!-- 显示消息 -->
    <%
        String successMessage = (String) session.getAttribute("successMessage");
        if (successMessage != null) {
            session.removeAttribute("successMessage");
    %>
    <div class="alert alert-success"><%= successMessage %>
    </div>
    <% } %>

    <%
        String errorMessage = (String) session.getAttribute("errorMessage");
        if (errorMessage != null) {
            session.removeAttribute("errorMessage");
    %>
    <div class="alert alert-danger"><%= errorMessage %>
    </div>
    <% } %>

    <!-- 操作按钮 -->
    <div class="action-buttons">
        <a href="${pageContext.request.contextPath}/admin/bookManage?method=showAddForm"
           class="btn btn-success">添加新书籍</a>
    </div>

    <!-- 搜索框 -->
    <div class="search-box">
        <form action="${pageContext.request.contextPath}/admin/book" method="get">
            <input type="hidden" name="method" value="bookManage">
            <input type="text" name="keyword" placeholder="输入书名、作者或ISBN搜索..." id="searchInput"
                   value="${keyword}">
            <button type="submit">搜索</button>
        </form>
    </div>

    <!-- 批量删除表单 -->
    <form action="${pageContext.request.contextPath}/admin/bookManage" method="post" class="batch-form"
          onsubmit="return confirmBatchDelete()">
        <input type="hidden" name="method" value="batchDelete">
        <button type="submit" class="btn btn-danger">批量删除选中项</button>

        <!-- 书籍表格 -->
        <table class="book-table">
            <thead>
            <tr>
                <th><input type="checkbox" id="selectAll" onclick="toggleSelectAll()"></th>
                <th>ISBN</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>出版日期</th>
                <th>总数量</th>
                <th>可借数量</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <%
                PagePO<BookPO> bookPagePO = (PagePO<BookPO>) request.getAttribute("bookPagePO");
                List<BookPO> bookList = bookPagePO.getList();
                if (bookList != null && !bookList.isEmpty()) {
                    for (BookPO bookPO : bookList) {
                        Book book = bookPO.getBook();
            %>
            <tr>
                <td><input type="checkbox" name="selectedBooks" value="<%= book.getId() %>" class="book-checkbox"></td>
                <td><%= book.getISBN() %>
                </td>
                <td><%= book.getBname() %>
                </td>
                <td><%= book.getAuthor() %>
                </td>
                <td><%= book.getPublisher() %>
                </td>
                <td><%= book.getPubDate() %>
                </td>
                <td><%= bookPO.getCount() %>
                </td>
                <td><%= bookPO.getCount() - bookPO.getBorrowCount() %>
                </td>
                <td>
                    <a href="${pageContext.request.contextPath}/admin/bookManage?method=getBookDetail&id=<%= book.getId() %>"
                       class="action-link">详情</a>
                    <a href="${pageContext.request.contextPath}/admin/bookManage?method=showEditForm&id=<%= book.getId() %>"
                       class="action-link">编辑</a>
                    <a href="${pageContext.request.contextPath}/admin/bookManage?method=deleteBook&id=<%= book.getId() %>"
                       class="action-link" onclick="return confirm('确定要下架这本书吗？')">删除</a>
                </td>
            </tr>
            <%
                }
            } else {
            %>
            <tr>
                <td colspan="9" style="text-align: center;">暂无数据</td>
            </tr>
            <% } %>
            </tbody>
        </table>
    </form>

    <!-- 分页 -->
    <div class="pagination">
        <%
            int pageNum = bookPagePO.getPageNum();
            int totalPages = bookPagePO.getTotalPage();
            String keyword = (String) request.getAttribute("keyword");
            String keywordParam = (keyword != null && !keyword.isEmpty()) ? "&keyword=" + keyword : "";

            // 上一页链接
            if (pageNum > 1) {
        %>
        <a href="${pageContext.request.contextPath}/admin/book?method=bookManage&pageNum=<%= pageNum - 1 %><%= keywordParam %>">&laquo;</a>
        <% } else { %>
        <a href="javascript:void(0)" class="disabled">&laquo;</a>
        <% } %>

        <%
            // 显示页码，最多显示5个页码
            int startPage = Math.max(1, pageNum - 2);
            int endPage = Math.min(totalPages, startPage + 4);

            // 调整startPage，确保显示5个页码（如果有足够的页数）
            if (endPage - startPage < 4 && startPage > 1) {
                startPage = Math.max(1, endPage - 4);
            }

            for (int i = startPage; i <= endPage; i++) {
        %>
        <a href="${pageContext.request.contextPath}/admin/book?method=bookManage&pageNum=<%= i %><%= keywordParam %>" <%= (i == pageNum) ? "class='active'" : "" %>><%= i %>
        </a>
        <% } %>

        <!-- 下一页链接 -->
        <% if (pageNum < totalPages) { %>
        <a href="${pageContext.request.contextPath}/admin/book?method=bookManage&pageNum=<%= pageNum + 1 %><%= keywordParam %>">&raquo;</a>
        <% } else { %>
        <a href="javascript:void(0)" class="disabled">&raquo;</a>
        <% } %>
    </div>

    <!-- 分页信息 -->
    <div style="text-align: center; margin-top: 10px;">
        共 <%= bookPagePO.getTotalCount() %> 条记录，当前第 <%= pageNum %> / <%= totalPages %> 页
    </div>
</div>

<script>
    // 全选/取消全选
    function toggleSelectAll() {
        var checkboxes = document.getElementsByClassName('book-checkbox');
        var selectAllCheckbox = document.getElementById('selectAll');

        for (var i = 0; i < checkboxes.length; i++) {
            checkboxes[i].checked = selectAllCheckbox.checked;
        }
    }

    // 确认批量删除
    function confirmBatchDelete() {
        var selectedBooks = [];
        var checkboxes = document.getElementsByClassName('book-checkbox');

        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                selectedBooks.push(checkboxes[i].value);
            }
        }

        if (selectedBooks.length === 0) {
            alert('请至少选择一本书籍');
            return false;
        }

        return confirm('确定要下架选中的 ' + selectedBooks.length + ' 本书吗？');
    }
</script>
</body>
</html>